﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace phpBB
{
    /// <summary>
    /// PhPBBRoleProvider that utilizes the phpBBDatabaseInterface.
    /// The provider is ReadOnly. Roles can not be created or deleted.
    /// The role membership of users can not be changed.
    /// Those actions have to be made in the phpBB administration backend.
    /// </summary>
    class phpBBRoleProvider : RoleProvider
    {
        /// <summary>
        /// phpBBDatabaseInterface that is used to perform the actions.
        /// </summary>
        private phpBBDatabaseInterface DatabaseInterface
        {
            get;
            set;
        }

        public override string ApplicationName
        {
            get;
            set;
        }

        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config must not be null.");
            }

            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "phpBB Role Provider");
            }

            base.Initialize(name, config);

            if (config["applicationName"] != null)
            {
                ApplicationName = config["applicationName"];
            }
            
            DatabaseInterface = phpBBDatabaseInterface.getSingleton();
            DatabaseInterface.Initialize(config);
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            return DatabaseInterface.IsUserInRole( username, roleName );
        }

        public override string[] GetRolesForUser(string username)
        {
            return DatabaseInterface.GetRolesForUser( username );
        }

        public override void CreateRole(string roleName)
        {
            throw new NotSupportedException("phpBBRoleProvider is readonly");
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotSupportedException("phpBBRoleProvider is readonly");
        }

        public override bool RoleExists(string roleName)
        {
            return DatabaseInterface.RoleExists(roleName);
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotSupportedException("phpBBRoleProvider is readonly");
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotSupportedException("phpBBRoleProvider is readonly");
        }

        public override string[] GetUsersInRole(string roleName)
        {
            return DatabaseInterface.GetUsersInRole(roleName);
        }

        public override string[] GetAllRoles()
        {
            return DatabaseInterface.GetAllRoles();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            return DatabaseInterface.FindUsersInRole(roleName, usernameToMatch);
        }
    }
}
